
package hk.ces.oxfam.patch.patchs;

import hk.ces.oxfam.patch.DBPatch;
import hk.ces.oxfam.patch.db.SQLUtil;
import hk.ces.oxfam.patch.db.result.ExcuteResult;
import hk.ces.oxfam.patch.db.sql.PreparedSQL;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 *
 * @author ReutyCheng
 */
public class PersonInfoTypeToWalkerPatch implements DBPatch
{
  private static final String WALKER = "WALKER";
  
  public String getName()
  {
    return "Update Person Info Type to [WALKER] Patch";
  }

  public void patch(Connection connection) throws SQLException
  {
    int walkerUpdatedResult = this.updateRestPersonInfoToWalker(connection);
    Logger.getLogger(this.getClass()).debug("Totally update Walker[count=" + walkerUpdatedResult + "]");
  }
  
  private int updateRestPersonInfoToWalker(Connection connection) throws SQLException
  {
    String sql = "update personinfo set personinfotype = ? where personinfotype is null or personinfotype = ''";
    Object[] objects = new Object[] {
      WALKER
    };

    ExcuteResult result = SQLUtil.execute(connection, new PreparedSQL(sql, objects));
    Logger.getLogger(this.getClass()).debug(result.getExcuteStatus() + " : Patch other Person with PersonInfoType is [WALKER]");
    return result.getResultCount();
  }
}
